/*
 * Class
 * ParameterTable
 */
package pl.abstractvoid.datamodel.parameters;

import java.io.Serializable;
import java.util.ArrayList;
import pl.abstractvoid.languages.LanguageProvider;

/**
 * Represents a parameter of a probability distribution that has a form of a
 * table.
 * <p>
 * For example, for the polynomial distribution such table can by a vector of
 * pairs (number of results of kind i; probability of a result of kind i).
 *
 * @author Wojciech Szałapski
 * @author Ivo Zieliński
 */
public class ParameterTable implements Serializable {

    /**
     * The serial number for serialization and deserialization with different 
     * versions of the application.
     */
    static final long serialVersionUID = 1L;
    /**
     * Name of the table for access from R scripts.
     */
    private final String tableName;

    /**
     * Name of the table for user interface.
     */
    private final String visibleTableName;

    /**
     * Names of the parameters stored in the table.
     * <p>
     * These names are reserved for access from R scripts.
     */
    private final String[] parameterNames;

    /**
     * Returns the hash of the parameter table.
     * 
     * @return Hash of the whole parameter table.
     */
    public double getHash() {
        double hash = 0;
        int columnNo = 1;
        for (ArrayList<Double> list : parameterValues) {
            for (double d : list) {
                hash += d * Math.pow(13, columnNo);
            }
            columnNo++;
        }
        return hash;
    }
    /**
     * Values of the parameters stored in the table.
     * <p>
     * The first dimension is of length equal to the number of parameters while
     * the second dimension pertains to the number of stored values and should
     * be equal for each ArrayList in the first dimension.
     */
    private final ArrayList<ArrayList<Double>> parameterValues;

    /**
     * Constructor initializing all needed names of the parameters, both visible
     * and for access in R functions.
     *
     * @param tableName Name of the table for access from R scripts.
     * @param visibleTableName Name of the table for user interface.
     * @param parameterNames Names of the parameters for access from R scripts.
     */
    public ParameterTable(String tableName, String visibleTableName,
            String[] parameterNames) {
        this.tableName = tableName;
        this.visibleTableName = visibleTableName;
        this.parameterNames = parameterNames;
        parameterValues = new ArrayList<>();
        for (int i = 0; i < parameterNames.length; ++i) {
            parameterValues.add(new ArrayList<Double>());
            for (int j = 0; j < 20; j++) {
                parameterValues.get(i).add(0d + i + j);
            }
        }
    }

    /**
     * Return all the values stored in the ParameterTable.
     *
     * @return Values stored in the ParameterTable in a form of 'two
     * dimensional' ArrayList in which the first dimension represents the
     * variable and the second represents it's value.
     */
    public ArrayList<ArrayList<Double>> getParameterValues() {
        return parameterValues;
    }

    /**
     * Returns names of the parameters stored in the table.
     *
     * @return Array of the names of parameters stored in the table in order
     * accordant to the first dimension of the ArrayList of values. These names
     * are meant to be used in R scripts.
     */
    public String[] getParameterNames() {
        return parameterNames;
    }

    /**
     * Returns the name of the table.
     *
     * @return Name of the table for access from R scripts.
     */
    public String getTableName() {
        return tableName;
    }

    /**
     * Returns the visible name of the table.
     *
     * @return Name of the table for user interface.
     */
    public String getVisibleTableName() {
        return visibleTableName;
    }

    /**
     * Returns visible names of parameters stored in the table.
     *
     * @return Visible names of parameters stored in the table in order
     * accordant to the first dimension of the ArrayList of values. These names
     * are meant to be used in user interface.
     */
    public String[] getVisibleParameterNames() {
        String[] visibleParameterNames = new String[parameterNames.length];
        for (int i = 0; i < parameterNames.length; ++i) {
            visibleParameterNames[i] = LanguageProvider.getInstance().
                    getDistributionString(parameterNames[i]);
        }
        return visibleParameterNames;
    }

    /**
     * Sets values of the parameters by replacing a parameter vector, i. e. the
     * first dimension in the parameterValues ArrayList.
     *
     * @param position Position of the vector to set in parameterValues
     * ArrayList.
     * @param valuesVector ArrayList of doubles representing a new vector of
     * values.
     */
    public void setParameterValues(int position, ArrayList<Double> valuesVector) {
        parameterValues.set(position, valuesVector);
    }
}
